package com.danger.Model.action;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.danger.Model.bean.User;
import com.danger.Model.bean.UserInfo;
import com.danger.Model.listener.OnLoginListener;
import com.danger.Util.CheckCode;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.LogInListener;

/**
 * Created by danger on 2016/3/17.
 */
public class LoginModelImpl implements LoginModel {

    private Context context;
    private String USERNAME_IS_NULL = "用户名为空";
    private String PASSWORD_IS_NULL = "密码为空";
    private String IS_RIGHT = "正确";

    public LoginModelImpl(Context context) {
        this.context = context;
    }

    @Override
    public void login(final User user, final OnLoginListener listener) {
        BmobUser.loginByAccount(context, user.getUsername(), user.getPassword(), new LogInListener<UserInfo>() {
            @Override
            public void done(UserInfo userInfo, BmobException e) {
                if (e == null) {
                    listener.onSuccess(userInfo);
                } else {
                    listener.onError(CheckCode.checkByCode(e.getErrorCode()));
                }
            }
        });
    }

    @Override
    public String check(User user) {
        if (TextUtils.isEmpty(user.getUsername().toString().trim())) {
            return USERNAME_IS_NULL;
        }
        if (TextUtils.isEmpty(user.getPassword().toString().trim())) {
            return PASSWORD_IS_NULL;
        }
        return IS_RIGHT;
    }
}
